<?
//показываем все ошибки
ini_set('display_errors' , 'On');
ini_set('error_reporting' , E_ALL);

//директория с библиотеками
define('DIR_LIBS', dirname(__FILE__).'/libs/');

//#################################################
//Пути для подключения
//#################################################
set_include_path(implode(PATH_SEPARATOR, array(DIR_LIBS,DIR_LIBS.'PEAR/',)));

//Автозагрузка классов
function __autoload($class){
    $path = str_replace('_','/',$class).'.php';
    foreach (explode(PATH_SEPARATOR,get_include_path()) as $dir) {
//        debug($dir.$path);
        if(file_exists($dir.$path)){
            include_once($dir.$path);
            return true;
        }
    }
    return false;
}

function _debug_backtrace($show=false){
    $data = '';
    foreach( debug_backtrace() as $trace){
        $file     = isset($trace['file'])
        ? $trace['file']
        : 'no file (eval/obfuscate/lambda function)';
        $line     = isset($trace['line'])
        ? $trace['line']
        : 'no line (eval/obfuscate/lambda function)';
        $function = isset($trace['function']) ? '<span style="color: rgb(0, 0, 187);">'.htmlspecialchars($trace['function']).'</span>' : 'no function';
        $class    = isset($trace['class'])    ? '<span style="color: rgb(0, 0, 187);">'.htmlspecialchars($trace['class']).'</span>'    : '';
        $type     = isset($trace['type'])     ? '<span style="color: rgb(0, 0, 187);">'.htmlspecialchars($trace['type']).'</span>'     : '';
        $file = htmlspecialchars($file);
        $data .= '--------------------------------------------------------------------------------
';
        $data .= '
<span style="color: rgb(0, 0, 0);"><strong>'.str_pad('['.$line.']', 5).'</strong> '.str_replace($_SERVER['DOCUMENT_ROOT'],'...',$file).'</span>
';

        if($class){
            $data .= $class.$type.$function.'(';
        } else {
            $data .= $function.'(';
        }
        if(isset($trace['args'])&& is_array($trace['args']) && count($trace['args'])){
            $data .= '<span style="color: rgb(221, 0, 0);">';
            if(1==count($trace['args'])){
                if(is_scalar($trace['args'][0])){
                    $data .= htmlspecialchars($trace['args'][0]);
                } else {
                    $data .= htmlspecialchars(gettype($trace['args'][0]));
                }
            } else {
                $data .= '
';
                $i=1;
                foreach ($trace['args'] as $arg) {
                    if(is_scalar($arg)){
                        $data .= '   '.$i.') '.htmlspecialchars($arg).'
';
                    } else {
                        $data .= '   '.$i.') '.htmlspecialchars(gettype($arg)).'
';
                    }
                    $i++;
                }
            }
            $data .= '</span>';
        }
        $data .= ');
';

    }
    if($show){
        print '<pre style="font-family:Tahoma">';
        print $data;
        print '</pre>';
    }
    return $data;
}


function debug($var='',$name='jQuickForm Debug',$need_print=true){
    static $item_number;
    if(!isset($item_number))$item_number=0;
    $item_number++;
    $vDebug = debug_backtrace();
    $vTraceStr = '<pre>'._debug_backtrace().'</pre>';
    $debug_message = '<style>pre{margin:0;padding:0;}</style>';
    $debug_message .= '<div style="font-size:12px;-moz-border-radius:10px;color:#FF0033;margin:5px auto;margin-bottom:10px;padding:10px;font-family:Verdana;background-color:#fceaee;border:1px solid #FF7C00;line-height:1.3em;overflow:auto;">';
    //заголовок
    if('array'==gettype($var)){
        $title = 'array('._plural(count($var),'элемент','элемента','элементов').')';
    } else {
        $title = (gettype ($var));
    }
    $debug_message .= '<h3 style="display:inline;margin:0;padding:0;text-align:center;color:#0180DA" onclick="javascript:var debug_inf=getElementById(\'debug_'.$item_number.'\');debug_inf.style.display=(debug_inf.style.display==\'none\')?\'block\':\'none\';">'.$name.' ('.$title.') &raquo;</h3>';
    //обертка содержимого
    $debug_message .= '<div style="margin:0;padding:0;display:'.(!is_scalar($var)?'none':'block').'" id="debug_'.$item_number.'">';
    $debug_message .= '<div style="margin:0;padding:0;"><a style="display:inline;color:#666666"></a>';
    $debug_message .= '<fieldset style="-moz-border-radius:10px;margin-top:5px;color:#999999"><legend onclick="javascript:var debug_files=getElementById(\'debug_files_'.$item_number.'\');debug_files.style.display=(debug_files.style.display==\'none\')?\'block\':\'none\';">Подключаемые файлы (кликни, чтобы открыть блок)</legend><div id="debug_files_'.$item_number.'" style="display:none;">'.$vTraceStr.'</div></fieldset></div>';
    $value = (!is_bool($var)) ? print_r($var,1) : ($var ? 'TRUE':'FALSE');
    $debug_message .= "<div style=\"padding:5px 0 0 20px;border-left:3px solid #FF7C00;margin:5px 0;\"><pre>".$value."</pre></div>";
    //обертка содержимого
    $debug_message .= '</div>';

    $debug_message .= '<div style="clear:both;"></div></div>';
    if($need_print){
        print $debug_message;
    }
    return $debug_message;
}


function _plural($cnt, $form1, $form2, $form5, $need_number=1,$form0=''){
    if(!$cnt){
        if($form0) return $form0;
    }
    $n = abs($cnt) % 100;
    $n1 = $cnt % 10;
    if ($n > 10 && $n < 20) return ($need_number ? $cnt.' ' : '').$form5;
    if ($n1 > 1 && $n1 < 5) return ($need_number ? $cnt.' ' : '').$form2;
    if ($n1 == 1) return ($need_number ? $cnt.' ' : '').$form1;
    return ($need_number ? $cnt.' ' : '').$form5;
}

?>